﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Text.RegularExpressions;

namespace HSE.Common
{
    public static class Util
    {
        public static byte[] ReadFile(FileStream fs)
        {
            byte[] buffer = new byte[fs.Length];
            try
            {
                fs.Read(buffer, 0, buffer.Length);
                fs.Seek(0, SeekOrigin.Begin);
                return buffer;
            }
            catch
            {
                return buffer;
            }
            finally
            {
                if (fs != null)
                    fs.Close();
            }
        }

        public static MemoryStream GetMemoryStream(byte[] pReadByte)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                ms.Write(pReadByte, 0, pReadByte.Length);
                //fs.Write(pReadByte, 0, pReadByte.Length);
            }
            catch
            {
                return ms;
            }
            finally
            {
                //if (fs != null)
                //    fs.Close();
            }
            return ms;
        }

        public static BitmapImage GetBitmapImage(Stream sm)
        {
            BitmapImage img = new BitmapImage();
            img.SetSource(sm);
            return img;
        }

        //public static Object DeserializeXML<T>(string xmlString)
        //{
        //    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString));
        //    XmlSerializer xml = new XmlSerializer(typeof(T));
        //    T O = (T)xml.Deserialize(ms);//xml反序列化的关键代码
        //    ms.Close();
        //    return O;

        //}

        public static object JsonToObject<T>(string jsonString)
        {

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

            System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonString));

            return serializer.ReadObject(mStream);

        }

        public static string ObjectToJson(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractJsonSerializer serializer =
                        new DataContractJsonSerializer(obj.GetType());
                serializer.WriteObject(ms, obj);
                ms.Position = 0;
                string temp= string.Empty;
                using (StreamReader reader = new StreamReader(ms))
                {
                    temp = reader.ReadToEnd();
                }

                //替换Json的Date字符串    
                string p = @"\\/Date/((/d+)/+/d+/)\\/"; /*////Date/((([/+/-]/d+)|(/d+))[/+/-]/d+/)////*/
                MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
                Regex reg = new Regex(p);
                temp = reg.Replace(temp, matchEvaluator);
                return temp;
            }

            //System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(obj.GetType());

            //string szJson = "";

            ////序列化

            //using (MemoryStream stream = new MemoryStream())
            //{
            //    serializer.WriteObject(stream, obj);
            //    szJson = Encoding.UTF8..GetString(stream.ToArray());
            //}
            //return szJson;

        }


        /// <summary>    
        /// JSON序列化    
        /// </summary>    
        //public static string JsonSerializer<T>(T t)
        //{
        //    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        //    MemoryStream ms = new MemoryStream();
        //    ser.WriteObject(ms, t);
        //    string jsonString = Encoding.UTF8.GetString(ms.ToArray());
        //    ms.Close();
        //    //替换Json的Date字符串    
        //    string p = @"///Date/((/d+)/+/d+/)///"; /*////Date/((([/+/-]/d+)|(/d+))[/+/-]/d+/)////*/
        //    MatchEvaluator matchEvaluator = new MatchEvaluator(ConvertJsonDateToDateString);
        //    Regex reg = new Regex(p);
        //    jsonString = reg.Replace(jsonString, matchEvaluator);
        //    return jsonString;
        //}

        /// <summary>    
        /// 将Json序列化的时间由/Date(1294499956278+0800)转为字符串    
        /// </summary>    
        private static string ConvertJsonDateToDateString(Match m)
        {
            string result = string.Empty;
            DateTime dt = new DateTime(1970, 1, 1);
            dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
            dt = dt.ToLocalTime();
            result = dt.ToString("yyyy-MM-dd HH:mm:ss");
            return result;
        }
        /// <summary>    
        /// 将时间字符串转为Json时间    
        /// </summary>    
        private static string ConvertDateStringToJsonDate(Match m)
        {
            string result = string.Empty;
            DateTime dt = DateTime.Parse(m.Groups[0].Value);
            dt = dt.ToUniversalTime();
            TimeSpan ts = dt - DateTime.Parse("1970-01-01");
            result = string.Format("//Date({0}+0800)//", ts.TotalMilliseconds);
            return result;
        }    

        //public static byte[] ObjectToByte(object obj)
        //{
        //    //BinaryFormatter se = new BinaryFormatter(); 
        //    //MemoryStream memStream = new MemoryStream(); 
        //    //se.Serialize(memStream, obj);
        //    //byte[] bobj = memStream.ToArray(); 
        //    //memStream.Close(); 
        //    //return bobj;
        //}
    }
}
